package com.study.springboot.common.mvc.validate;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author wei.zhao
 * @description:
 * @date 2021年12月27日 17:35
 */
public class PhoneValidator implements ConstraintValidator<PhoneValidate, String> {

    private static final int PHONE_LENGTH = 11;

    private static final Pattern pattern = Pattern.compile("^[1]\\d{10}$");

    @Override
    public boolean isValid(String phone, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(phone) || phone.length() != PHONE_LENGTH || !pattern.matcher(phone).matches()) {
            return false;
        }
        return true;
    }

}
